package com.example.demo.service.impl;

import com.example.demo.entity.Sign;
import com.example.demo.entity.User;
import com.example.demo.exception.BusyException;
import com.example.demo.exception.sign.SignAllException;
import com.example.demo.exception.sign.SignExitException;
import com.example.demo.exception.sign.SignNotExitException;
import com.example.demo.mapper.SignMapper;
import com.example.demo.service.SignService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SignServiceImpl implements SignService {
    @Resource
    SignMapper signMapper;

    @Override
    public void createSign(Sign sign) {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        Sign exitSign=signMapper.selectByName(sign.getName(),username);
        int res=0;
        if(exitSign!=null){
            throw new SignExitException();
        }else{
            sign.setUsername(username);
            res=signMapper.insertSign(sign);
            if(res<=0){
                throw new BusyException();
            }
        }

    }

    @Override
    public void newSignFile(Sign sign) {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        sign.setUsername(username);
        sign.setPath(username+"/"+sign.getPath());
        Sign exitSign=signMapper.selectSign(sign.getName(),sign.getUsername(),sign.getPath());
        int res=0;
        if(exitSign!=null){
            throw new SignExitException();
        }else{
            res=signMapper.insertSignFile(sign);
            if(res<=0){
                throw new BusyException();
            }
        }
    }

    @Override
    public void delSignFile(Sign sign) {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        sign.setUsername(username);
        sign.setPath(username+"/"+sign.getPath());
        Sign exitSign=signMapper.selectSign2(sign);
        int res=0;
        if(exitSign==null){
            throw new SignNotExitException();
        }else{
            res=signMapper.deleteSignFile(sign);
            if(res<=0){
                throw new BusyException();
            }
        }
    }

    @Override
    public List<String> getFileSign(String path) {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        Sign sign=new Sign();
        sign.setUsername(username);
        sign.setPath(username+"/"+path);
        System.out.println(sign.getPath()+sign.getUsername());
        List<String> FileSign;
        FileSign=signMapper.selectSignAll(sign);
        if(FileSign==null){
            throw new SignAllException();
        }
        return FileSign;
    }

    @Override
    public List<Map> getAllSign() {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        List<Map> AllSign=signMapper.selectAllSign(username);
        if(AllSign==null){
            throw new SignAllException();
        }
        return AllSign;
    }

    @Override
    public void coverSign(String[] newSign,String path) {
        Subject subject= SecurityUtils.getSubject();
        User user =(User) subject.getPrincipal();
        String username=user.getUsername();
        path=username+"/"+path;
        Sign sign=new Sign();
        sign.setUsername(username);
        sign.setPath(path);
        int res=0;
        if(path==null){
            throw new SignNotExitException();
        }else{
            res=signMapper.deleteSignFile2(path,username);
            if(res<=0){
                throw new BusyException();
            }
        }
        for (int i = 0; i < newSign.length; i++) {
            sign.setName(newSign[i]);
            res=signMapper.insertSignFile(sign);
            if(res<=0){
                throw new BusyException();
            }
        }

    }
}
